﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MainWindow.xaml.cs" company="">
//   
// </copyright>
// <summary>
//   Interaction logic for MainWindow.xaml
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace StatusManager
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics.CodeAnalysis;
    using System.Windows;

    using AnotherJiraRestClient;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1650:ElementDocumentationMustBeSpelledCorrectly", Justification = "Reviewed. Suppression is OK here.")]
    public partial class MainWindow : Window
    {
        /// <summary>
        /// The settings dictionary.
        /// </summary>
        private Dictionary<string, string> settingsDictionary = new Dictionary<string, string>();

        /// <summary>
        /// Initializes a new instance of the <see cref="MainWindow"/> class.
        /// </summary>
        [SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1126:PrefixCallsCorrectly", Justification = "Reviewed. Suppression is OK here.")]
        public MainWindow()
        {
            InitializeComponent();
            settingsDictionary = Helper.ParseCredentialsFromXml(@"settings.xml");
        }

        /// <summary>
        /// The button_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        [SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1126:PrefixCallsCorrectly", Justification = "Reviewed. Suppression is OK here.")]
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var account = new JiraAccount
            {
                ServerUrl = settingsDictionary["Url"],
                User = settingsDictionary["Login"],
                Password = settingsDictionary["Password"]
            };

            Helper.CreateEmail(settingsDictionary["OutlookLogin"], settingsDictionary["OutlookPassword"]);
            
            bool today = Convert.ToBoolean(TodayCheckbox.IsChecked);
            string startDate;
            string jqlString;
            string outputString;
            var outputList = new List<string>();
            DateTime dateTime = DateTime.Now;
            string datePatt = @"yyyy-MM-dd";

            if (!today)
            {
                dateTime = DateTime.Now.AddDays(-1);
            }

            startDate = dateTime.ToString(datePatt);
            jqlString = "created >= " + startDate + " AND" + " reporter = currentUser()";

            var client = new JiraClient(account);
            var issues = client.GetIssuesByJql(
                jqlString,
                0,
                25,
                new string[]
                    {
                        Issue.FIELD_SUMMARY, 
                        Issue.FIELD_STATUS,
                        Issue.FIELD_PRIORITY
                    });

            foreach (var issue in issues.issues)
            {
                outputString = issue.key + " \"" + issue.fields.summary + " \"";
                outputList.Add(outputString);
            }

        }
    }
}
